package me.sdevil507.supports.shiro.matcher;

import me.sdevil507.biz.common.sms.captcha.exceptions.CaptchaInvalidException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.springframework.util.StringUtils;

/**
 * 加密手机号登录验证匹配Matcher
 *
 * @author sdevil507
 */
public class PhoneNumberMatcher extends SimpleCredentialsMatcher {


    /**
     * 重写凭证验证方法
     *
     * @param token 令牌
     * @param info  信息
     * @return boolean
     */
    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        // 获取手机登录名
        String phoneNumber = (String) token.getPrincipal();
        if (!StringUtils.isEmpty(phoneNumber)) {
            // 如果验证成功
            return true;
        } else {
            // 如果验证失败
            throw new CaptchaInvalidException("手机号无效!");
        }
    }
}
